'use strict';

module.exports = app => {

  class ActivitesService extends app.Service {

    constructor(ctx) {
      super(ctx);
      this.result = {
        err: '',
        code: -1,
        data: null,
      };
    }

    async index() {
      const activitiesList = await app.model.Activites.findAll({
        include: {
          model: this.app.model.User,
        },
      }, { limit: 10 }, { order: 'created_at DESC' }
      );

      this.result.data = activitiesList;
      return this.result;
    }

    async query(page = 1, size = 10) {
      const activitiesList = await app.model.Activites.findAll({
        include: {
          model: this.app.model.User,
        },
      }, { limit: size * page }, { order: 'created_at DESC' }
      );
      this.result.data = activitiesList;

      return this.result;
    }

    async info(id) {
      const result = await app.model.Activites.findById(id, {
        include: {
          model: this.app.model.User,
        },
      }, { limit: 10 }, { order: 'created_at DESC' });
      if (result) {
        this.result.data = result;
      } else {
        this.result.err = '该活动已经被下线,或设置不能查看';
      }
      return this.result;
    }


    async create(activity) {
      const newUser = await app.model.Activites.create(activity);
      return this.result;
    }


    async edit(id, acvitiy) {

    }


    async destroy(id) {

    }
  }

  return ActivitesService;

};
